package cc.ycws.api.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cc.ycws.core.domain.*;
import cc.ycws.core.service.*;
import com.alibaba.fastjson.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/ssm/v1")
public class PadApi {

    @Resource
    UnitdoorService unitdoorService;
    @Resource
    UploadService uploadService;
    @Resource
    VisitorsService visitorsService;
    @Resource
    CommunicationService communicationService;
    @Resource
    CountService countService;
    @Resource
    FailService failService;
    @Resource
    AccessrecordService accessrecordService;
    @Resource
    LocationService locationService;

    /*
     * 获取用户信息
     */
    @ResponseBody
    @RequestMapping(value = "/wwluserinfo", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
    public String wwluserinfo(HttpServletRequest request, ModelMap map) {

        Date data = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stoptime = dateFormat.format(data);
        String starttime = request.getParameter("starttime");
        String idnumber = request.getParameter("idnumber");

        Accessrecord access = accessrecordService.query(idnumber);
        Accessrecord accessrecord = new Accessrecord();
        if (access != null) {
            accessrecord.setPadnumber(idnumber);
            accessrecord.setThistime(stoptime);
            accessrecordService.update(accessrecord);
        } else {
            accessrecord.setPadnumber(idnumber);
            accessrecord.setThistime(stoptime);
            accessrecordService.insert(accessrecord);
        }

        Unitdoor unitdoor = unitdoorService.selectIdnumber(idnumber);
        if (unitdoor.getNumber().equals("all")) {
            Map<String, Object> maps = new HashMap<String, Object>();
            maps.put("starttime", starttime);
            maps.put("stoptime", stoptime);
            maps.put("yaddress", unitdoor.getAddress());
            /*maps.put("ydetailed", "");*/

            Map<String, Object> maps1 = new HashMap<String, Object>();
            maps1.put("starttime", starttime);
            maps1.put("stoptime", stoptime);
            maps1.put("yaddress", unitdoor.getAddress());
            /*maps1.put("yunitdoor", "");*/

            List<Uploadfile> list = uploadService.selectTimeAndUnitdoorAllto(maps);
            List<Visitors> visitors = visitorsService.selectTimeAndUnitdoorAllto(maps1);

            Map<String, Object> fail0 = new HashMap<>();
            fail0.put("usertype", "0");
            fail0.put("pad", idnumber);

            Map<String, Object> fail1 = new HashMap<>();
            fail1.put("usertype", "1");
            fail1.put("pad", idnumber);

            List<Uploadfile> listfail = uploadService.fail(fail0);            //业主拉取失败的数据
            List<Visitors> visitorsfail = visitorsService.fail(fail1);        //访客拉取失败的数据

            map.addAttribute("list", list);
            map.addAttribute("visitors", visitors);
            map.addAttribute("listfail", listfail);
            map.addAttribute("visitorsfail", visitorsfail);
            map.addAttribute("systemtime", stoptime);
            JSONObject json = new JSONObject(map);

            Map<String, Object> del = new HashMap<>();
            del.put("pad", idnumber);
            failService.delete(del);                                //删除失败数据
            return json.toString();
        } else {

            Map<String, Object> maps = new HashMap<String, Object>();
            maps.put("starttime", starttime);
            maps.put("stoptime", stoptime);
            maps.put("yaddress", unitdoor.getAddress());
            maps.put("ydetailed", unitdoor.getNumber());
            maps.put("all", "0");

            Map<String, Object> maps1 = new HashMap<String, Object>();
            maps1.put("starttime", starttime);
            maps1.put("stoptime", stoptime);
            maps1.put("yaddress", unitdoor.getAddress());
            maps1.put("yunitdoor", unitdoor.getNumber());
            maps1.put("all", "0");

            List<Uploadfile> list = uploadService.selectTimeAndUnitdoor(maps);
            List<Visitors> visitors = visitorsService.selectTimeAndUnitdoor(maps1);

            Map<String, Object> fail0 = new HashMap<>();
            fail0.put("usertype", "0");
            fail0.put("pad", idnumber);

            Map<String, Object> fail1 = new HashMap<>();
            fail1.put("usertype", "1");
            fail1.put("pad", idnumber);

            List<Uploadfile> listfail = uploadService.fail(fail0);            //业主拉取失败的数据
            List<Visitors> visitorsfail = visitorsService.fail(fail1);        //访客拉取失败的数据

            map.addAttribute("list", list);
            map.addAttribute("visitors", visitors);
            map.addAttribute("listfail", listfail);
            map.addAttribute("visitorsfail", visitorsfail);
            map.addAttribute("systemtime", stoptime);
            new JSONObject();
            JSONObject json = new JSONObject(map);
            Map<String, Object> del = new HashMap<>();
            del.put("pad", idnumber);
            failService.delete(del);                                //删除失败数据
            return json.toString();
        }
    }

    /**
     * 心跳接口
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/wwlcommunication", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String wwlcommunication(HttpServletRequest request) {
        String idnumber = request.getParameter("idnumber");
        Communication comm = communicationService.show(idnumber);
        if (comm != null) {
            //当前系统时间为结束时间
            Date data = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String stoptime = dateFormat.format(data);
            comm.setId(comm.getId());
            comm.setAccesstime(stoptime);
            communicationService.update(comm);
            return "{state" + ":1}";
        } else {
            return "{state" + ":0}";
        }
    }

    /**
     * 心跳接口1
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/wwlcommunication1", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String wwlcommunication1(HttpServletRequest request, ModelMap map) {
        String idnumber = request.getParameter("idnumber");
        Communication comm = communicationService.show(idnumber);
        if (comm != null) {
            //当前系统时间为结束时间
            Date data = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String stoptime = dateFormat.format(data);
            comm.setId(comm.getId());
            comm.setAccesstime(stoptime);
            communicationService.update(comm);
            return "{state" + ":1}";
        } else {
            return "{state" + ":0}";
        }
    }

    /**
     * 统计次数
     *
     * @param request
     * @param count
     * @return
     */
    @RequestMapping(value = "/wwlcount", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public String wwlcount(HttpServletRequest request, Count count) {
        System.out.println("进入刷脸");
        String id = request.getParameter("id");
        String datetime = request.getParameter("datetime");
        String idnumber = request.getParameter("idnumber");
        String doortype = request.getParameter("doortype");
        String usertype = request.getParameter("usertype");
        int number = Integer.parseInt(id);
        count.setId(number);
        count.setIdnumber(idnumber);
        count.setDatetime(datetime);
        count.setDoortype(doortype);
        count.setUsertype(usertype);
        int num = countService.insert(count);
        if (num == 1) {
            //消息推送
				/* if(usertype.equals("0")){
				 		Uploadfile uploadfile=countService.OwnerPush(id);
				 		Location location=locationService.messagepush(uploadfile.getYaddress());
				 		if(location.getMessagepush().equals("1")){
				 			if(uploadfile.getYphone().length() == 11 && uploadfile.getUserid().length() == 32){
				 				System.out.println("开始:"+DataUtil.data2());
				 				System.out.println("id："+id);
				 				System.out.println("手机号码："+uploadfile.getYphone());
				 				System.out.println("姓名："+uploadfile.getYname());
					 			Map<String, String> mapParam = new HashMap<String, String>();
						        mapParam.put("mobile", uploadfile.getYphone());
						        mapParam.put("title","e安全");
						        mapParam.put("content","温馨提示:"+uploadfile.getYname()+"回家了。");
						        mapParam.put("soucre","1");
						        mapParam.put("sign","d60d4f003a7df267f235445b8abf2c49");
						        mapParam.put("ts","1517193355");
						        mapParam.put("appID","ICEEANQUAN-ADC5-42B8-BB2E-BC39B211FDED");
						        String result= Http.doPost("https://openapi.colourlife.com/v1/czyprovide/pushInformation", mapParam, "utf-8");
						        System.out.println(result);
						        System.out.println("结束:"+DataUtil.data2());
						 }
				 	}
				 }else if(usertype.equals("1")){
				 		Visitors visitors=countService.VisitorsPush(id);
				 		Location location=locationService.messagepush(visitors.getYaddress());
				 		if(location.getMessagepush().equals("1")){
				 			if(visitors.getYphone().length() == 11 && visitors.getUserid().length() == 32){
					 			Map<String, String> mapParam = new HashMap<String, String>();
					 	        mapParam.put("mobile", visitors.getYphone());
					 	        mapParam.put("title","e安全");
					 	        mapParam.put("content","温馨提示:"+visitors.getYname()+"回家了。");
					 	        mapParam.put("soucre","1");
					 	        mapParam.put("sign","d60d4f003a7df267f235445b8abf2c49");
					 	        mapParam.put("ts","1517193355");
					 	        mapParam.put("appID","ICEEANQUAN-ADC5-42B8-BB2E-BC39B211FDED");
					 	        String result= Http.doPost("https://openapi.colourlife.com/v1/czyprovide/pushInformation", mapParam, "utf-8");
						        System.out.println(result);
						 }
				 	}
				 }*/
            return "{state" + ":1}";
        } else {
            return "{state" + ":0}";
        }
    }

    /**
     * 失败记录
     *
     * @param request
     * @param count
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/wwlfail", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
    public String wwlfail(HttpServletRequest request, Fail fail) {
        String id = request.getParameter("id");
        String pad = request.getParameter("pad");
        String failtype = request.getParameter("failtype");
        String usertype = request.getParameter("usertype");
        if (failtype.equals("-6")) {
            int id1 = Integer.parseInt(id);
            fail.setId(id1);
            fail.setPad(pad);
            fail.setFailtype(failtype);
            fail.setUsertype(usertype);
            int num = failService.insert(fail);
            if (num == 1) {
                return "{state" + ":1}";    //成功
            } else {
                return "{state" + ":0}";    //失败
            }
        } else {
            int id1 = Integer.parseInt(id);
            fail.setId(id1);
            fail.setPad(pad);
            fail.setFailtype(failtype);
            fail.setUsertype(usertype);
            int num = failService.insert2(fail);
            if (num == 1) {
                return "{state" + ":1}";    //成功
            } else {
                return "{state" + ":0}";    //失败
            }
        }
    }
}
